﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using System.Security;

namespace DeployGames.TweenEngine.TestBed.core
{
    public class Clock
    {
        [DllImport("kernel32.dll"), SuppressUnmanagedCodeSecurity]
        private static extern bool QueryPerformanceCounter(out long ticks);
        [DllImport("kernel32.dll"), SuppressUnmanagedCodeSecurity]
        private static extern bool QueryPerformanceFrequency(out long freq);

        private long myTmpFreq;
        private long myTmpCtr;
        private long myDeltaCtr;
        private long myDelta;

        public long Frequency
        {
            get
            {
                return myTmpFreq;
            }
        }
        public long Counter
        {
            get
            {
                QueryPerformanceCounter(out myTmpCtr);
                return myTmpCtr;
            }
        }

        public void Reset()
        {
            myDeltaCtr = Counter;
        }
        public long TicksDelta()
        {
            QueryPerformanceCounter(out myTmpCtr);
            myDelta = myTmpCtr - myDeltaCtr;
            myDeltaCtr = myTmpCtr;
            return myDelta;
        }
        public double Delta()
        {
            return (double)TicksDelta() / myTmpFreq;
        }

        public Clock()
        {
            QueryPerformanceFrequency(out myTmpFreq);
        }
    }
}
